package cn.iocoder.yudao.module.project.service.kismaterielexwarehouseorderedit;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.project.controller.admin.kismaterielexwarehouseorderedit.vo.*;
import cn.iocoder.yudao.module.project.dal.dataobject.kismaterielexwarehouseorderedit.KisMaterielExWarehouseOrderEditDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.project.dal.mysql.kismaterielexwarehouseorderedit.KisMaterielExWarehouseOrderEditMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.project.enums.ErrorCodeConstants.*;

/**
 * 订单编辑-删除相关物料出库单 Service 实现类
 *
 * @author wangzuoyang
 */
@Service
@Validated
public class KisMaterielExWarehouseOrderEditServiceImpl implements KisMaterielExWarehouseOrderEditService {

    @Resource
    private KisMaterielExWarehouseOrderEditMapper kisMaterielExWarehouseOrderEditMapper;

    @Override
    public Long createKisMaterielExWarehouseOrderEdit(KisMaterielExWarehouseOrderEditSaveReqVO createReqVO) {
        // 插入
        KisMaterielExWarehouseOrderEditDO kisMaterielExWarehouseOrderEdit = BeanUtils.toBean(createReqVO, KisMaterielExWarehouseOrderEditDO.class);
        kisMaterielExWarehouseOrderEditMapper.insert(kisMaterielExWarehouseOrderEdit);
        // 返回
        return kisMaterielExWarehouseOrderEdit.getRecycleId();
    }

    @Override
    public void updateKisMaterielExWarehouseOrderEdit(KisMaterielExWarehouseOrderEditSaveReqVO updateReqVO) {
        // 校验存在
        validateKisMaterielExWarehouseOrderEditExists(updateReqVO.getRecycleId());
        // 更新
        KisMaterielExWarehouseOrderEditDO updateObj = BeanUtils.toBean(updateReqVO, KisMaterielExWarehouseOrderEditDO.class);
        kisMaterielExWarehouseOrderEditMapper.updateById(updateObj);
    }

    @Override
    public void deleteKisMaterielExWarehouseOrderEdit(Long id) {
        // 校验存在
        validateKisMaterielExWarehouseOrderEditExists(id);
        // 删除
        kisMaterielExWarehouseOrderEditMapper.deleteById(id);
    }

    private void validateKisMaterielExWarehouseOrderEditExists(Long id) {
        if (kisMaterielExWarehouseOrderEditMapper.selectById(id) == null) {
            throw exception(KIS_MATERIEL_EX_WAREHOUSE_ORDER_EDIT_NOT_EXISTS);
        }
    }

    @Override
    public KisMaterielExWarehouseOrderEditDO getKisMaterielExWarehouseOrderEdit(Long id) {
        return kisMaterielExWarehouseOrderEditMapper.selectById(id);
    }

    @Override
    public PageResult<KisMaterielExWarehouseOrderEditDO> getKisMaterielExWarehouseOrderEditPage(KisMaterielExWarehouseOrderEditPageReqVO pageReqVO) {
        return kisMaterielExWarehouseOrderEditMapper.selectPage(pageReqVO);
    }

}